package com.erp.generator.config;

import com.erp.generator.GeneratorCode;
import com.erp.generator.util.TableToClassUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Data
public class ServiceConfig {

    public static String jdbcUrl;
    public static String jdbcUserName;
    public static String jdbcPassword;
    public static String jdbcDriverName;
    public static String outPath;
    public static String[] tableNames;
    public static String tablePrefix;
    public static String packageName;
    public static String author;
    public static String program;
    public static String moduleName;
    public static String serviceGenerate;
    public static Map<String, String> classNameMap = new HashMap<>();

    static {
        try {
            initProperties();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void initProperties() throws IOException {
        Properties properties = new Properties();
        InputStream in = GeneratorCode.class.getClassLoader().getResourceAsStream("generator/application.properties");
        properties.load(in);
        String tableStr = properties.getProperty("gen.tableNames","");
        packageName = properties.getProperty("gen.packageName","");
        jdbcUrl = properties.getProperty("jdbc.url");
        jdbcUserName = properties.getProperty("jdbc.username");
        jdbcPassword = properties.getProperty("jdbc.password");
        jdbcDriverName = properties.getProperty("jdbc.driverName");
        if(!tableStr.isEmpty()) {
            tableNames = tableStr.split(",");
        }
        outPath = properties.getProperty("gen.outPath");
        author = properties.getProperty("gen.author");
        program = properties.getProperty("gen.program");
        moduleName = properties.getProperty("gen.moduleName");
        serviceGenerate = properties.getProperty("gen.generate");

        //表前缀
        tablePrefix = properties.getProperty("jdbc.tablePrefix");

        for (String tableName : tableNames) {
//            String key = TableToClassUtil.changeToClassName(tableName);
//            String value = TableToClassUtil.changeToJavaFiled(tableName);

            String reTableName ;
            if(StringUtils.isNotBlank(tablePrefix)){
                //去前缀
                reTableName = tableName.replaceFirst(tablePrefix,"");
            }else{
                reTableName = tableName;
            }
            String key = TableToClassUtil.changeToClassName(reTableName);
            String value = TableToClassUtil.changeToJavaFiled(reTableName);
            classNameMap.put(key,value);
        }
    }

}
